#!/usr/bin/env python
#
#   Creates a Model-view combo with the following command:
#   makeMV -c com_myComponent -m myModel -t controller
#
#   It will generate the following:
#       com_myComponent/models/myModel.php
#       com_myComponent/views/myModel/view.html.php
#       com_myComponent/views/myModel/tmpl/default.php
#
#   If -i is provided, dummy index files are placed like so:
#       com_myComponent/views/myModel/index.html
#       com_myComponent/views/myModel/tmpl/index.html
#
import optparse
import os

#
# Model file
# ------------------------------------------------------------------------------
modelFile = """
<?php
/**
 *  
 *  
 * @package %s
 * @author Daniel Torres
 */
 
// Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access');

jimport('joomla.application.component.model');

/**
 *  
 *  
 * @author Daniel Torres
 */
 class %sModel%s extends JModel
 {
    /**
     *  Private vars
     */
            
 }
?>
"""

# View file
# ------------------------------------------------------------------------------

viewFile = """
<?php
/**
 * 
 *  
 * @package %s
 * @author Daniel Torres
 */
 
 // Make sure we have access to this page
 defined('_JEXEC') or die ('Restricted access. Sorry');
 
 jimport('joomla.application.component.view');
 
 /**
 * 
 *  
 * @author Daniel Torres
 */
 class %sView%s extends JView
 {
    /**
     *  Prepares the information needed to be displayed
     */
    function display( $tpl = null )
    {
        parent::display( $tpl );
    }    
 }
?>
"""
# Template file
# ------------------------------------------------------------------------------

defaultFile = """
<?php
/**
 * 
 *  
 * @package %s
 * @author Daniel Torres
 */
 
 // Make sure we have access to this page
 defined('_JEXEC') or die ('Restricted access. Sorry');
?>
<h1>Empty template file</h1>
"""
# dummy html file
# ------------------------------------------------------------------------------
htmlFile = "<html><body></body></html>"


# Generate the option parser
parser = optparse.OptionParser()
parser.add_option("-t", "--controller", dest="controller", help="controller name")
parser.add_option("-m", "--model", dest="model", help="Model name")
parser.add_option("-p", "--package", dest="package", help="package name", default="[Package]")
parser.add_option("-i", "--index", dest="index", action="store_true", default=False, help="Adds an index file to all new directories" )
parser.add_option("-a", "--admin", dest="admin", action="store_true", default=False, help="Adds files on the administration directory")

# parse arguments
(options,args) = parser.parse_args();
if options.model == None or options.controller == None:
    print "Wrong option number, both -m and -c are required"

    
# Get the base directory
if options.admin == True:
    baseDir = "admin" 
else:
    baseDir = "site" 
        
# Create the required directories
path = "%s/views/%s" % (baseDir,options.model)
print "Generating directory: %s" % path 
os.mkdir( path )
    
print "Generating directory: %s/tmpl" % path
os.mkdir("%s/tmpl" % path)
        
# Model
path = "%s/models/%s.php" % (baseDir,options.model)
print "Generating model file: %s" % path    
file = open( path,"w")
file.write(modelFile % (options.package,options.controller,options.model))
file.close()
    
# Views
path = "%s/views/%s/view.html.php" % (baseDir,options.model)
print "Generating view file: %s" % path
file = open( path, "w" )
file.write(viewFile % (options.package,options.controller,options.model))
file.close()
    
# Template
path = "%s/views/%s/tmpl/default.php" % (baseDir,options.model)
print "Generating template file: %s" % path    
file = open(path, "w")
file.write(defaultFile % options.package )
file.close()
    
# Add index files?
if options.index is True:
    print "Generating index files..."
        
    path = "%s/views/%s/index.html" % (baseDir,options.model) 
    print "\t%s" % path
    file = open(path, "w")
    file.write(htmlFile)
    file.close()
        
    path ="%s/views/%s/tmpl/index.html" % (baseDir,options.model)
    print "\t%s" % path
    file = open(path, "w")
    file.write(htmlFile)
    file.close()
    
print "Done"
    
    